<template>
    <div>
        <div class="OrderSettlement">
            <div class="OrderSettlement-Header">
                <van-nav-bar title="确认订单" left-arrow @click="back" />
            </div>
            <div class="OrderSettlement-Body">
                <div class="OrderSettlement-Address" @click="popupshow = !popupshow">
                    <div class="Index-Address-Msg">
                        <div class="Address-Msg">
                            <div class="Address-site one-txt-cut">
                                {{ list[Number(chosenAddressId) - 1].address }}
                            </div>
                            <div class="Address-NAP">
                                <h3 class="Address-Name"> {{ list[Number(chosenAddressId) - 1].name }}</h3>
                                <span class="Address-phone"> {{ list[Number(chosenAddressId) - 1].tel }}</span>
                            </div>
                        </div>
                        <div class="Address-MsgIcon">
                            <van-icon name="arrow" />
                        </div>
                    </div>
                </div>
                <!-- 弹出层 -->
                <van-popup v-model:show="popupshow" position="bottom" :style="{ height: '60%' }">
                    <van-address-list default-tag-text="默认" switchable :list="list" v-model="chosenAddressId" />
                </van-popup>

                <div class="OrderSettlement-CardBox">
                    <van-card :num="item.count" :price="item.price" :desc="item.name" :title="item.marque" :thumb="item.img"
                        v-for="(item, index) in SubmitData" :key="index" />
                    <div class="OrderSettlement-price">
                        共{{ SubmitData.length }}件商品 合计：
                        <span>￥{{ total }}</span>
                    </div>
                </div>
            </div>
            <div class="OrderSettlement-footer">
                <van-submit-bar :ShopPrice="1000" button-text="付款" @click="pay" />
            </div>
            <div style="height: 50px;"></div>
        </div>

    </div>
</template>
<script lang="ts">
import { defineComponent, ref, computed } from 'vue';
import { orderStore, carListStore, historyOrderStore } from '@/stores/shopCar'
import router from '@/router';
import { showToast } from 'vant';
export default defineComponent({
    setup() {
        const chosenAddressId = ref('1');

        const pay = () => {
            showToast('付款成功')
            router.back()
            historyOrderStore().submitOrder({ itemIist: orderStore().$state.order, money: total })
            carListStore().$state.carList.forEach((item: any) => {
                for (let i in orderStore().$state.order) {
                    console.log(orderStore().$state.order[i].id);
                }

            })
            for (let i = 0; i < carListStore().$state.carList.length; i++) {
                for (var j in orderStore().$state.order) {
                    if (carListStore().$state.carList[i].id == orderStore().$state.order[j].id) {
                        carListStore().$state.carList.splice(i, 1)
                    }
                }
            }
        }
        const list = [
            {
                id: '1',
                name: '张三',
                tel: '13000000000',
                address: '浙江省杭州市西湖区文三路 138 号东方通信大厦 7 楼 501 室',
                isDefault: true,
            },
            {
                id: '2',
                name: '李四',
                tel: '1310000000',
                address: '浙江省杭州市拱墅区莫干山路 50 号',
            },
            {
                id: '3',
                name: '张三5',
                tel: '13000000000',
                address: '浙江省杭州市西湖区文三路 138 号东方通信大厦 7 楼 501 室',

            },
        ];

        const back = () => {
            router.back()
        }
        let SubmitData = orderStore().$state.order
        let popupshow = ref(false)
        const total: any = computed(() => {
            return SubmitData.reduce((totalMoney: number, item: any): any => {
                if (item.check) {
                    return totalMoney + (item.count * item.price)
                } else {
                    return totalMoney
                }
            }, 0)
        })
        return {
            popupshow,
            SubmitData,
            back,
            total, chosenAddressId, list, pay
        }
    }
})

</script>
<style lang="scss">
.van-submit-bar {
    bottom: 0px !important;
}

.OrderSettlement {
    height: 100vh;
    background: #f2f2f2;

    .OrderSettlement-Header {
        margin-bottom: 10px;

        .van-nav-bar {
            // background: transparent;

            .van-nav-bar__content {
                .van-nav-bar__left {
                    i {
                        color: #000
                    }
                }
            }
        }
    }

    .OrderSettlement-Body {
        // height: 80vh;
        margin: 0 6px;

        .OrderSettlement-Address {
            padding: 8px 14px;
            background: #fff;
            border-radius: 12px;
            height: 46px;
            margin-bottom: 10px;

            .Index-Address-Msg {
                display: flex;
                align-items: center;
                justify-content: space-between;

                .Address-Msg {
                    overflow: hidden;
                }

                .Address-site {
                    font-size: 16px;
                    font-weight: bold;
                    margin-bottom: 8px;
                }

                .Address-NAP {

                    display: flex;
                    align-items: center;
                    font-size: 14px;
                    color: #5f5f5f;

                    .Address-Name {
                        margin-right: 7px;
                        font-size: 14px;
                    }

                    .Address-phone {
                        display: block;
                    }
                }
            }
        }

        .OrderSettlement-CardBox {
            border-radius: 12px;
            background: #fff;

            .van-card {
                background-color: #fff;
                border-radius: 12px;
            }

            .OrderSettlement-price {
                display: flex;
                justify-content: flex-end;
                margin-right: 10px;
                font-size: 14px;
                padding: 5px 0;

                span {
                    color: #c00;
                }

                ;
            }
        }
    }
}
</style>